package com.springboot.multiplethread.controller;

import com.alibaba.fastjson2.JSONObject;
import com.springboot.multiplethread.config.ThreadPoolConfig;
import com.springboot.multiplethread.service.MyService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * 控制层
 *
 * @author sam
 */
@RestController
@Slf4j
public class MyController {

    @Resource
    private MyService myService;

    @GetMapping("/multipleThreadTest")
    public Map<String, Object> testMultipleThreadTest() {

        Future<JSONObject> future1 = myService.queryViaMultipleThread1();

        Future<JSONObject> future2 = myService.queryViaMultipleThread2();

        Future<JSONObject> future3 = myService.queryViaMultipleThread3();

        Map<String, Object> res = new HashMap<>();
        try {
            res.put("future1", future1.get().toString());
            res.put("future2", future2.get().toString());
            res.put("future3", future3.get().toString());
        } catch (Exception e) {
            log.error("Exception:", e);
        }

        return res;
    }

    @GetMapping("mockProduct")
    public Map<String, Object> mockProduct() {
        /**串行代码示例**/
//        // 假设查订单
//        Map<String, Object> orders = myService.queryOrder();
//        // 假设查库存
//        Map<String, Object> inventory = myService.queryInventory();
//        // 假设查商品信息
//        Map<String, Object> items = myService.queryItems();
//
//        Map<String, Object> data = new HashMap<>();
//        data.put("orders", orders);
//        data.put("items", items);
//        data.put("inventory", inventory);
//        return data;
        /**串行代码示例**/

        /**异步编排代码示例**/
        return myService.queryAsyn();
        /**异步编排代码示例**/
    }

}
